package com.ntanh.mealplanner.main.planner;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ntanh.mealplanner.R;
import com.ntanh.mealplanner.base.model.BaseEntity;
import com.ntanh.mealplanner.base.model.NamedEntity;
import com.ntanh.mealplanner.main.objects.DayMeal;
import com.ntanh.mealplanner.main.objects.Meal;
import com.ntanh.mealplanner.main.objects.Recipe;

/**
 * Created by Anh Nguyen on 3/2/2015.
 */
public class DayMealExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<DayMeal> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<DayMeal, List<BaseEntity>> _listDataChild;

    private static final int[] EMPTY_STATE_SET = {};
    private static final int[] GROUP_EXPANDED_STATE_SET =
            {android.R.attr.state_expanded};
    private static final int[][] GROUP_STATE_SETS = {
            EMPTY_STATE_SET, // 0
            GROUP_EXPANDED_STATE_SET // 1
    };

    DateFormat format = new SimpleDateFormat("EEEE (dd/MM/yyyy)");

    public DayMealExpandableListAdapter(Context context, List<DayMeal> listDataHeader, HashMap<DayMeal, List<BaseEntity>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (this._listDataChild.size() > 0) {
            return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
        } else {
            return null;
        }
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.day_meal_list_item, null);
        }

        final View view = convertView;
        // get the listview
        final ListView listView = (ListView) convertView.findViewById(R.id.mealLv);

        final List<String> listDataHeader = new ArrayList<String>();
        Object child = getChild(groupPosition,childPosition);
        if(child != null){
            if(child instanceof Meal){
                listDataHeader.add(((Meal) child).getType().getName());
            } else if(child instanceof Recipe){
                listDataHeader.add("    " + ((Recipe) child).getName());
            }
        }
        MealListAdapter listAdapter = new MealListAdapter(_context, R.layout.meal_list_item, listDataHeader);

        // setting list adapter
        listView.setAdapter(listAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // ListView Clicked item index
                int itemPosition = position;
                // ListView Clicked item value
                String itemValue = (String) listView.getItemAtPosition(position);

            }
        });

        ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
        layoutParams.height = (int) 150 * listDataHeader.size();
        listView.setLayoutParams(layoutParams);

        convertView.invalidate();

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        try {
            if (this._listDataChild.size() > 0) {
                return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
            } else {
                return 0;
            }
        }catch(Exception e){
            return 0;
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) {
        String headerTitle = format.format(((DayMeal)getGroup(groupPosition)).getDate());
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.day_meal_list_group, null);
        }

        final ExpandableListView expLv = (ExpandableListView) parent.findViewById(R.id.expLvDayMeal);
        final int position = groupPosition;

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblDay);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        View ind = convertView.findViewById(R.id.explist_indicator);
        if (ind != null) {
            ImageView indicator = (ImageView) ind;
            indicator.setVisibility(View.VISIBLE);
            int stateSetIndex = (isExpanded ? 1 : 0);
            Drawable drawable = indicator.getDrawable();
            drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
            indicator.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //  expand the ListView Group/s
                    if (!isExpanded) {
                        //  API Level 14+ allows you to animate the Group expansion...
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                            expLv.expandGroup(position, true);
                        } else {  //  else just expand the Group without animation
                            expLv.expandGroup(position);
                        }
                    } else {//  collapse the ListView Group/s
                        expLv.collapseGroup(position);
                    }
                }
            });
        }

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
